﻿namespace UnityEditorInternal
{
    using System;
    using UnityEditor;
    using UnityEngine;

    public class MacroEvents
    {
        public static Event CrossPlatformSwapDefaultModifiers(Event evt)
        {
            return new Event(evt) { control = evt.command, command = evt.control };
        }

        public static string EventToString(Event evt)
        {
            object[] args = new object[] { evt.type, evt.mousePosition.x, evt.mousePosition.y, evt.delta.x, evt.delta.y, evt.button, (int) evt.modifiers, evt.pressure, evt.clickCount, (int) evt.character, evt.keyCode };
            return string.Format("type: {0} mousePosition: {1},{2} delta: {3},{4} button: {5} modifiers: {6} pressure: {7} clickCount: {8} character: {9} keyCode: {10}", args);
        }

        private static Vector2 ParseVector2(string vec)
        {
            char[] separator = new char[] { ',' };
            string[] strArray = vec.Split(separator);
            return new Vector2(float.Parse(strArray[0]), float.Parse(strArray[1]));
        }

        public static void RecordMacroEvent(System.Type windowType, Event evt)
        {
            if ((Macros.enabled && Macros.recordEvents) && ((((evt.type == EventType.MouseDown) || (evt.type == EventType.MouseUp)) || ((evt.type == EventType.MouseDrag) || (evt.type == EventType.KeyDown))) || (((evt.type == EventType.KeyUp) || (evt.type == EventType.ValidateCommand)) || (evt.type == EventType.ExecuteCommand))))
            {
                string str2;
                string fullName = windowType.FullName;
                switch (fullName)
                {
                    case "UnityEditorInternal.MacroWindow":
                        return;

                    case "UnityEditor.ConsoleWindow":
                        return;
                }
                if (Application.platform == RuntimePlatform.OSXEditor)
                {
                    str2 = "UnityEditorInternal.MacroEvents.AutoConvert.FromOSX";
                }
                else
                {
                    str2 = "UnityEditorInternal.MacroEvents.AutoConvert.FromWindows";
                }
                Macros.AddGenericMacroInstruction(string.Format("UnityEditorInternal.MacroEvents.SendMacroEvent(\"{0}\", \"{1}\", {2});", fullName, EventToString(evt), str2));
                if (evt.type == EventType.MouseUp)
                {
                    Macros.AddGenericMacroInstruction("Unsupported.SceneTrackerFlushDirty();");
                }
            }
        }

        public static void SendMacroEvent(string targetName, string eventString)
        {
            SendMacroEvent(targetName, eventString, AutoConvert.None);
        }

        public static void SendMacroEvent(string targetName, string eventString, AutoConvert autoConvert)
        {
            if (Unsupported.GetTypeFromFullName(targetName) == null)
            {
                Debug.LogError("Failed to send recorded event because there is type named: " + targetName);
            }
            else
            {
                UnityEngine.Object[] objArray = Resources.FindObjectsOfTypeAll(Unsupported.GetTypeFromFullName(targetName));
                if (objArray.Length == 0)
                {
                    Debug.LogError("Failed to send recorded event because there is no window of type: " + targetName);
                }
                else
                {
                    UnityEngine.Object obj2 = objArray[0];
                    EditorWindow window = obj2 as EditorWindow;
                    if (window == null)
                    {
                        Debug.LogError("Failed to send recorded event because the target does not derive from EditorWindow: " + targetName);
                    }
                    else
                    {
                        Event evt = StringToEvent(eventString);
                        if ((autoConvert == AutoConvert.FromOSX) && (Application.platform == RuntimePlatform.WindowsEditor))
                        {
                            evt = CrossPlatformSwapDefaultModifiers(evt);
                        }
                        else if ((autoConvert == AutoConvert.FromWindows) && (Application.platform == RuntimePlatform.OSXEditor))
                        {
                            evt = CrossPlatformSwapDefaultModifiers(evt);
                        }
                        if (!window.SendEvent(evt))
                        {
                            Debug.LogError("Failed to send recorded event because the event could not be forwarded to the parent view: " + targetName + " // " + eventString);
                        }
                    }
                }
            }
        }

        public static Event StringToEvent(string input)
        {
            Event event2 = new Event();
            string[] separator = new string[] { "type: ", " mousePosition: ", " delta: ", " button: ", " modifiers: ", " pressure: ", " clickCount: ", " character: ", "keyCode: " };
            string[] strArray2 = input.Split(separator, StringSplitOptions.None);
            event2.type = (EventType) ((int) Enum.Parse(typeof(EventType), strArray2[1]));
            event2.mousePosition = ParseVector2(strArray2[2]);
            event2.delta = ParseVector2(strArray2[3]);
            event2.button = int.Parse(strArray2[4]);
            event2.modifiers = (EventModifiers) int.Parse(strArray2[5]);
            event2.pressure = float.Parse(strArray2[6]);
            event2.clickCount = int.Parse(strArray2[7]);
            event2.character = (char) int.Parse(strArray2[8]);
            event2.keyCode = (KeyCode) ((int) Enum.Parse(typeof(KeyCode), strArray2[9]));
            return event2;
        }

        public enum AutoConvert
        {
            None,
            FromOSX,
            FromWindows
        }
    }
}

